/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.ucue.ptc.gco.dao;

import edu.ucue.ptc.gco.mdl.Cliente;
import java.util.List;
import javax.ejb.Stateless;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Valex
 */
@Stateless
@Named(value = "clienteDao")
public class ClienteDAO implements ClienteDAOLocal {

    @PersistenceContext(unitName = "edu.ucue.gped_gestionpedidosee-ejb_ejb_1.0-SNAPSHOTPU")
    private EntityManager entityManager;

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    @Override
    public Cliente crearCliente(Cliente cliente) {
        entityManager.persist(cliente);
        return cliente;
    }

    @Override
    public Cliente actualizarCliente(Cliente cliente) {
        entityManager.merge(cliente);
        return cliente;
    }

    @Override
    public String eliminar(Cliente cliente) {
        entityManager.remove(cliente);
        return "ok";
    }

    @Override
    public List<Cliente> listar() {
        Query query = entityManager.createQuery("select c from Cliente c where c.estado = 0");
        return query.getResultList();        
    }

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    @Override
    public Cliente buscarPorCedula(String cedula) {
        List<Cliente> lc = listarClienteWhere("where o.cedulaRuc='" + cedula + "'");
        if (lc.isEmpty()) {
            return null;
        } else {
            return (Cliente) lc.get(0);
        }
    }

    @Override
    public Cliente buscarPorId(int id) {
        List<Cliente> lc = listarClienteWhere("where o.idPersona='" + String.valueOf(id) + "'");
        if (lc.isEmpty()) {
            return null;
        } else {
            return (Cliente) lc.get(0);
        }
    }

    @Override
    public List<Cliente> listarClienteWhere(String where) {
        Query query = entityManager.createQuery("select object(o) from Cliente as o " + where + " and o.estado = 0");
        return query.getResultList();        
    }

    @Override
    public List<Cliente> verificarCliente(String id) {
        Query query = entityManager.createQuery("select object(o) from Cliente as o  where o.cedulaRuc='" + id + "'");
        return query.getResultList();
    }

    @Override
    public String eliminarPorCedula(String cedula) {
        Cliente cliente = buscarPorCedula(cedula);
        if (cliente != null) {
            entityManager.remove(cliente);
        }
        return "ok";
    }

    @Override
    public String eliminarPorId(int id) {
        Cliente cliente = buscarPorId(id);
        if (cliente != null) {
            entityManager.remove(cliente);
        }
        return "ok";
    }
    
    @Override
    public String eliminarCliente(Cliente empleado) {
        empleado.setEstado(1);
        entityManager.merge(empleado);
        return "ok";
    }
}
